PATHMac OS 8 and 9 Developer Documentation > Interapplication Communication > AppleScript for Scripters >

AppleScript Language Guide

   

Repeat With (loopVariable) From (startValue) To (stopValue)

In the Repeat With (loopVariable) From (startValue) To (stopValue) form of the Repeat statement, the loop variable is an integer that is increased by a specified value after each iteration of the loop. The loop terminates when the value of the variable is greater than a predefined stop value.

SYNTAX
repeat with loopVariable from startValue to stopValue [ by stepValue ]
    [ statement ]...
end [ repeat ]

where

loopVariable is used to control the number of iterations. It can be any previously defined variable or a new variable you define in the Repeat statement (see "Notes").

startValue (an integer) is the value assigned to loopVariable when the loop is entered.

stopValue (an integer) is the value of loopVariable at which iteration ends. Iteration continues until the value of loopVariable is greater than the value of stopValue.

stepValue (an integer) is the value added to loopVariable after each iteration of the loop. The default value of stepValue is 1.

statement is any AppleScript statement.

EXAMPLE

The following example numbers the paragraphs of a document with the Repeat With (loopVariable) From (startValue) To (stopValue) form of the Repeat statement.

tell document "Simple" of application "AppleWorks"
    set numParagraphs to (count paragraphs of text body)
    repeat with n from 1 to numParagraphs
        set paragraph n of text body to (n as string) ¬
            & " " & paragraph n of text body
    end repeat
end tell
NOTES

You can use an existing variable as the loop variable in a Repeat statement or define a new one in the Repeat statement. In either case, the loop variable is defined outside the loop. You can change the value of the loop variable inside the loop body but it will get reset to the next loop value the next time through the loop. After the loop completes, the loop variable retains its last value.

AppleScript evaluates startValue. stopValue. and stepValue when it begins executing the loop and stores the values internally. If you change the values in the body of the loop, it has no effect on the execution of the loop.


© 1999 Apple Computer, Inc. – (Last Updated 21 May 99)